package com.yb.personelmanagement.service.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.yb.personelmanagement.domain.System.Role;
import com.yb.personelmanagement.domain.System.RoleRepository;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	RoleRepository roleRepository;
	@Override
	public List<Role> findAll() {
		
		return roleRepository.findAll();
	}

	@Override
	public List<Role> findSearch(Map searchMap) {
		
		return roleRepository.findAll(createSpecification(searchMap));
	}

	@Override
	public Page<Role> findSearch(Map searchMap, int page, int size) {
		// TODO Auto-generated method stub
		return null;
	}

	
	private Specification<Role> createSpecification(Map searchMap){
		return new Specification<Role>() {
		@Override
		public Predicate toPredicate(Root<Role> root, CriteriaQuery<?>
		criteriaQuery, CriteriaBuilder cb) {
		List<Predicate> predicateList=new ArrayList<>();
		if(searchMap.get("roleName")!=null &&
		!"".equals(searchMap.get("roleName"))){
		predicateList.add(cb.like(
		root.get("roleName").as(String.class), "%"+
		(String)searchMap.get("roleName")+"%" ) );
		}
		
		return cb.and( predicateList.toArray( new
		Predicate[predicateList.size()]) );
		}
		};
		}
	
	
}
